package com.liyuci.core.controller;

import com.liyuci.core.service.IService;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 基础控制器.
 * @author liyuci
 * @date Oct 18, 2017 11:19:57 PM
 */
@Slf4j
public class BaseController<T>
{
	@Autowired
	public IService<T> service;

	private String getControllerRequestMappingPath()
	{
		RequestMapping annotation = (RequestMapping)this.getClass().getAnnotation(RequestMapping.class);
		if(annotation != null)
		{
			if(StringUtils.isAllEmpty(annotation.value()))
			{
				return "/";
			}
			return annotation.value()[0];
		}
		log.info(this.getClass().getSimpleName() + "::请配置@RequestMapping注解.");
		return "";
	}
	
	public String indexPage()
	{
		return toPage("index");
	}
	
	public String listPage()
	{
		return toPage("list");
	}
	
	public String addEditPage()
	{
		return toPage("addEdit");
	}
	
	public String toPage(String pageName)
	{
		return getControllerRequestMappingPath() + "/" + pageName;
	}
	
	/**
	 * 重定向URL，防止刷新页面会重新提交数据信息
	 * @author liyuci
	 * @date 2018-02-09 09:16:43
	 * @param url
	 * @return
	 */
	public String redirectURL(String url)
	{
		return "redirect:" + url;
	}
	
	/**
	 * 重定向URL
	 * @author liyuci
	 * @date 2018-02-09 09:17:27
	 * @param url
	 * @return
	 */
	public String forwardURL(String url)
	{
		return "forward:" + url;
	}
}
